using GrapeCity.Forguncy.Commands;
using GrapeCity.Forguncy.Plugin;
using GrapeCity.Forguncy.Log;
using System;
using System.ComponentModel;
using System.Threading.Tasks;
using System.Collections.Generic;

namespace FaissOperation
{
    [Icon("pack://application:,,,/FaissOperation;component/Resources/Icon.png")]
    [Designer("FaissOperation.Designer.FaissOperationServerCommandDesigner, FaissOperation")]
    [Category("嵌入式向量索引")]
    [OrderWeight(110)]
    public class Cluster_Remove : Command, ICommandExecutableInServerSideAsync
    {
        [FormulaProperty]
        [DisplayName("集合名")]
        public object KBName { get; set; }

        [FormulaProperty]
        [DisplayName("ID（Label）")]
		[Description("向量对应的唯一标识符，通常是文本的ID或索引。")]
		public object IDFormula { get; set; }

        [DisplayName("将处理结果保存到变量")]
        [Description("Gzip压缩后的文本，包含集合的全部数据。")]
        [ResultToProperty]
        public string ResultInParamater { get; set; } = "BASE64";

        public async Task<ExecuteResult> ExecuteAsync(IServerCommandExecuteContext dataContext)
        {
            var name = (await dataContext.EvaluateFormulaAsync(KBName))?.ToString() ?? "";
            var id = await dataContext.EvaluateFormulaAsync(IDFormula);

            if (string.IsNullOrEmpty(name))
            {
                throw new Exception("集合名不能为空，请检查输入。");
            }

            if (!CollectionManager.Instance.CollectionExists(name))
            {
                throw new Exception("当前向量集合尚未被加载，请先调用【加载向量集合】然后执行本操作。");
            }

            if (id == null)
            {
                throw new Exception("ID不能为空，请检查输入。");
            }

            // 获取指定名称的集合
            var collection = CollectionManager.Instance.GetCollection(name);

            // 从集合中移除指定ID的向量
            collection.RemoveVector(int.Parse(id.ToString()));

            // 将集合数据序列化为BASE64字符串
            dataContext.Parameters[ResultInParamater] = GeneralConverter.ToZipBase64(collection.GetData());

            return new ExecuteResult();
        }

        public override string ToString()
        {
            return "从集合中移除向量";
        }

        public override CommandScope GetCommandScope()
        {
            return CommandScope.ExecutableInServer;
        }

       
    }
}
